577B - Modulo Sum - CodeForces Solution


combinatorics data structures dp two pointers *1900

Please click on ads to support us..

Python Code:

from calendar import c
from cmath import inf
from inspect import stack
from math import ceil, gcd, sqrt
import os
import sys
from io import BytesIO, IOBase
from collections import Counter, defaultdict, deque
from heapq import heappush, heappop
sys.setrecursionlimit(10**5)


mod = 10 ** 9 + 7
mod1 = 998244353
BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def lcm(a, b):
    return a*b//gcd(a, b)




n,m=map(int,input().split())
a=set()
for i in map(int,input().split()):
    b=set()
    b = {(i+j)%m for j in a}
    a|=b
    a.add(i%m)
    if 0 in a:
        print('YES')
        exit()
print('NO')

C++ Code:

#include <bits/stdc++.h>

using namespace std;
#define name ""
const int N = 1e3 + 7 , maxm = 1e6  + 7;
long long a[N];
long long f[maxm];
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    if(fopen("bug.inp", "r"))
        freopen("bug.inp", "r", stdin), freopen("bug.out", "w", stdout);
    else if (fopen(name".inp", "r"))
        freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
    int n, m; cin >> n >> m;
    if(n > m) return cout << "YES" , 0;
    for(int i = 1 ; i <= n ; i++) {
        cin >> a[i] , a[i] %= m;
        if(a[i] % m == 0) return cout <<"YES" , 0;
    }
    f[0] = 1;
    for(int i = 1 ; i <= n ; i++){
        for(int w = maxm  ; w >= a[i] ; w--){
            if(f[w - a[i]]){
                f[w] = 1;
            }
        }
    }
    for(int i = m ; i <= maxm ; i+= m) if(f[i]) return cout <<"YES" ,0;
    cout <<"NO";
    return 0;
}


Comments

Submit
0 Comments
More Questions

1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes